package cn.autumnorange.app.common.dto;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Objects;

@Setter
@Getter
public abstract class BaseDto extends AbstractDto {
  private String name;

  //  protected <T extends BaseDto> JsonResult validationEntity(T dto) {
  //    String isVali = ValidationUtils.validation(dto);
  //    if (!isVali.equals(ResponseCodeConstans.SUCCESS)) {
  //      return JsonResult.putFail(isVali);
  //    }
  //    return null;
  //  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof BaseDto)) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    BaseDto baseDto = (BaseDto) o;
    return Objects.equals(name, baseDto.name);
  }

  @Override
  public int hashCode() {

    return Objects.hash(super.hashCode(), name);
  }

  //  protected boolean paramIDNameCheck(AbstractItem abstractItem) {
  //    if (!paramIdCheck(abstractItem.getId())) {
  //      return false;
  //    }
  //    return paramNameCheck(abstractItem.getName());
  //  }
  public ResponseEntity checkNumber(String errorParamName, String numberStr) {
    if (StringUtils.isBlank(numberStr)) {
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
          .body(ResultDto.createMessageResult(errorParamName + "不能为空"));
    }
    if (!StringUtils.isNumeric(numberStr)) {
      return ResponseEntity.ok(ResultDto.createMessageResult(errorParamName + "必须为大于1的正整数"));
    }
    return null;
  }

  public ResponseEntity checkName(String errorParamName) {
    return checkName(errorParamName, this.name, 255);
  }

  public ResponseEntity checkName(String errorParamName, String param, Integer length) {
    if (StringUtils.isBlank(param)) {

      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
          .body(ResultDto.createMessageResult(errorParamName + "不能为空"));
    }
    if (length != null) {
      if (param.length() > length) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
            .body(ResultDto.createMessageResult(errorParamName + "长度超过限制,长度不能超过" + length + "字符"));
      }
    }
    return null;
  }
}
